package com.minarto.utils {
	import flash.geom.*;

	/**
	 * @author KIMMINHWAN
	 */
	public class MathX {
		static public const TO_RADIAN:Number = Math.PI / 180,
							TO_ANGLE:Number = 180 / Math.PI,
							POINT:Point = new Point,
							RECTANGLE:Rectangle = new Rectangle,
							
							PLUS:String = "+",
							MINUS:String = "-",
							MULTI:String = "*",
							DIVISION:String = "/";
							
							
		static private var	_randoms:Vector.<Number> = new <Number>[],
							_c:int = - 1;				
		
		
		static public function random():Number{
			if (_c < 0) {
				_c = 350; 
				while (_c --) {
					_randoms.push(Math.random());
					_randoms.push(Math.random());
					_randoms.push(Math.random());
					_randoms.push(Math.random());
					_randoms.push(Math.random());
					_randoms.push(Math.random());
					_randoms.push(Math.random());
					_randoms.push(Math.random());
				}
				
				return	Math.random();
			}
			else {
				if (++ _c == 2800) {
					_c = 0;
				}
				return	_randoms[_c];
			}
		}
		
		
		static public function randomNumber($max:Number= 1, $min:Number = 0):Number{
			var min:Number, max:Number;
			
			if (isNaN($max)) {
				$max = 1;
			}
			$min = $min || 0;
			
			if ($max > $min) {
				min = $min;
				max = $max;
			}
			else {
				min = $max;
				max = $min;
			}
			
			return	random() * (max - min) + min;
		}
		
		
		static public function randomInt($max:int= 1, $min:int = 0):int{
			var min:int, max:int;
			
			if ($max > $min) {
				min = $min;
				max = $max;
			}
			else {
				min = $max;
				max = $min;
			}
			
			return	random() * (max - min) + min;
		}
		
		
		public static function decimalCal($n0:Number, $operator:String, $n1:Number):Number {
			var s0:String, s1:String, n0:Number, n1:Number, p:uint, r:Number;
			
			if(isNaN($n0) || isNaN($n1))	return	NaN;
			else if($operator != PLUS && $operator != MINUS && $operator != MULTI && $operator != DIVISION)	return	NaN;
			else if(!$n0 || !$n1) {
				s0 = "" + $n0;
				n0 = s0.indexOf(".", 0);
				if(n0 < 0)	n0 = s0.length;
				n0 = s0.length - n0;
				
				s1 = "" + $n1;
				n1 = s1.indexOf(".", 0);
				if(n1 < 0)	n1 = s1.length;
				n1 = s1.length - n1;
				
				p = Math.max(n0, n1);
				r = Math.pow(10, p);
				n0 = Number(s0.split(".").join("")) * r;
				n1 = Number(s1.split(".").join("")) * r;
				
				switch($operator){
				case PLUS :
					return	(n0 + n1) / r;
				case MINUS :
					return	(n0 - n1) / r;
				case MULTI :
					return	(n0 * n1) / (r * r);
				case DIVISION :
					return	n0 / n1;
				}
			}
			else if(!$n0){
				switch($operator){
				case PLUS :
					return	n1;
				case MINUS :
					return	n1;
				default :
					return	0;
				}
			}
			else {	//	$n1 === 0
				switch($operator){
				case MULTI :
					return	0;
				case DIVISION :
					return	$n0 < 0 ? - Infinity : Infinity;
				default :
					return	n0;
				}
			}
			
			return	NaN;
		}
		
		
		static public function decimalCals(...$params):Number{
			var i:uint, c:uint, r:Number;
			
			c = $params.length;
			if (c && !(c % 2)) {
			}
			else{
				r = $params[0];
				while((i += 2) < c){
					r = decimalCal(r, $params[i - 1], $params[i]);
				}
			}
			
			return  r;
		}
	}
}